package leetcode.leetcode100;

import java.util.LinkedList;
import java.util.Queue;

class ListNode{
    int val;
    ListNode next;
    ListNode(){
    }
    public ListNode(int val) {
        this.val = val;
    }
    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
public class addTwo {
    public static void main(String[] args) {
        ListNode listNode = new ListNode();
        ListNode listNode2 = new ListNode();
        int[] arr1 = {2,4,3};
        int[] arr2 = {5,6,4};
        for (int i = 0; i < arr1.length; i++) {
            listNode.val = arr1[i];
            if (i+1<arr1.length){
                ListNode nextNode = new ListNode();
                listNode.next = nextNode;
                listNode = nextNode;
            }
        }
        for (int i = 0; i < arr2.length; i++) {
            listNode2.val = arr1[i];
            if (i+1<arr2.length){
                ListNode nextNode = new ListNode();
                listNode2.next = nextNode;
                listNode2 = nextNode;
            }
        }
    }
    public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
        Queue<Integer> queue = new LinkedList<Integer>();
        ListNode listNode = new ListNode();
        ListNode ans = listNode;
        int one = 0;
        while (l1!=null && l2!=null){
            int cur = l1.val+l2.val+one;
            if (cur>=10){
                cur = cur%10;
                one = 1;
            }else {
                one = 0;
            }
           queue.offer(cur);
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1!=null){
            ListNode nextNode = new ListNode();
            int cur1 = l1.val+one;
            if (cur1>=10){
                cur1 = cur1%10;
                one = 1;
            }else {
                one = 0;
            }
            queue.offer(cur1);
            l1 = l1.next;
        }
        while (l2!=null){
            ListNode nextNode = new ListNode();
            int cur2 = l2.val+one;
            if (cur2>=10){
                cur2 = cur2%10;
                one = 1;
            }else {
                one = 0;
            }
            queue.offer(cur2);
            l2 = l2.next;
        }
        if (one ==1){
            queue.offer(1);
        }
        while (!queue.isEmpty()){
            listNode.val = queue.peek();
            if (!queue.isEmpty()){
               ListNode next =  new ListNode();
               listNode = next;
            }
        }
        return ans;
    }
}
